package morenoapp.components.welcome;

import java.awt.Dimension;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.EditorKit;
import morenoapp.action.MorenoActionsFacade;
import morenoapp.action.resources.ActionsResourcesHolder;
import morenoapp.components.common.BrowserHyperlinkListener;
import morenoapp.util.I18N;
import morenoapp.util.MorenoUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

/**
 *
 * @author dmitry.mamonov
 */
public class WelcomePanel extends JPanel {
    JButton buttonCreateNewResearch;
    JTextPane textRecentFiles;
    JTextPane textNews;
    JTextPane textWelcome;

    public WelcomePanel() {
        super();
        initComponents();
        initLayout();
    }

    private void initComponents() {
        { //create new research button
            JButton button = new JButton(MorenoActionsFacade.getActionCreateNewModel());
            button.setIcon(ActionsResourcesHolder.ICON_ACTION_NEW_128);
            button.setFont(button.getFont().deriveFont(24.0f));
            button.setText(I18N.get("welcome.create_new_research"));
            buttonCreateNewResearch = button;
        }
        { //recent files list
            
        }
        { //news list

        }
        { //welcome text
            JTextPane text = new JTextPane();
            text.setContentType("text/html");
            text.getDocument().putProperty("IgnoreCharsetDirective", Boolean.TRUE);
            text.setPreferredSize(new Dimension(300,600));
            text.addHyperlinkListener(new BrowserHyperlinkListener());
            try {
                InputStream pageDataStream = getClass().getResourceAsStream("welcome.html");
                if (pageDataStream!=null){
                    //instead of:
                    //this -> String html = IOUtils.toString(pageDataStream, "UTF-8");
                    //and this-> text.setText(html);
                    //we will use this to ensure text encoding:
                    Document doc = text.getDocument();
                    doc.remove(0, doc.getLength());
                    Reader r = new InputStreamReader(pageDataStream,"UTF-8");
                    EditorKit kit = text.getEditorKit();
                    kit.read(r, doc, 0);
                } else {
                    text.setText(I18N.get("welcome.error_no_welcome_page_resource"));
                }
            } catch (Exception ex) {
                text.setText(I18N.get("welcome.error_no_welcome_page")+"\n"+
                            MorenoUtil.toStirng(ex));
            }
            
            text.setEditable(false);
            
            textWelcome = text;
        }
    }

    private void initLayout() {
        add(new JScrollPane(textWelcome));
    }

    public static void main(String[] args) {
        MorenoUtil.debugShowComponentInFrame(new WelcomePanel());
    }
}
